package cn.edu.zsc.rbac.service.array;

import cn.edu.zsc.rbac.domain.dto.BaseDto;
import cn.edu.zsc.rbac.domain.dto.UserDto;
import cn.edu.zsc.rbac.domain.entity.BaseEntity;
import cn.edu.zsc.rbac.domain.vo.Result;
import cn.edu.zsc.rbac.domain.vo.UserVo;
import cn.edu.zsc.rbac.service.IUserService;

import java.util.ArrayList;
import java.util.List;

public class UserService implements IUserService {
    private static UserService singleton;
    private List<UserVo> userList = new ArrayList<>();

    private UserService() {
        // 初始化测试用户（实际可从数据库加载 ）
        userList.add(new UserVo(1, "testUser", "测试用户", "12345678901", "test@example.com", "研发部", "普通员工", true));
    }

    public static UserService getInstance() {
        if (singleton == null) {
            singleton = new UserService();
        }
        return singleton;
    }

    @Override
    public boolean existsById(int id) {
        return false;
    }

    @Override
    public BaseEntity findById(int id) {
        return null;
    }

    @Override
    public Result list() {
        Result result = new Result();
        result.success = true;
        result.message = "用户列表获取成功";
        result.data = userList.toArray(new UserVo[0]);
        return result;
    }

    @Override
    public Result create(BaseDto dto) {
        return null;
    }

    @Override
    public Result update(BaseDto dto, int id) {
        return null;
    }

    @Override
    public Result create(UserDto userDto) {
        UserVo newUser = new UserVo();
        newUser.setUsername(userDto.getUsername());
        newUser.setName(userDto.getName());
        newUser.setPhone(userDto.getPhone());
        newUser.setEmail(userDto.getEmail());
        // 这里需根据实际逻辑关联部门、角色名称，示例直接写死
        newUser.setDeptName("默认部门");
        newUser.setRoleName("默认角色");
        newUser.setEnabled(true);
        newUser.setId(userList.size() + 1);

        userList.add(newUser);

        Result result = new Result();
        result.success = true;
        result.message = "用户创建成功";
        result.data = newUser;
        return result;
    }

    @Override
    public Result toggle(int userId) {
        for (UserVo user : userList) {
            if (user.getId() == userId) {
                user.setEnabled(!user.isEnabled());
                Result result = new Result();
                result.success = true;
                result.message = "用户状态切换成功";
                return result;
            }
        }
        Result result = new Result();
        result.success = false;
        result.message = "用户不存在，状态切换失败";
        return result;
    }

    @Override
    public Result update(UserDto userDto, int userId) {
        for (UserVo user : userList) {
            if (user.getId() == userId) {
                user.setUsername(userDto.getUsername());
                user.setName(userDto.getName());
                user.setPhone(userDto.getPhone());
                user.setEmail(userDto.getEmail());
                // 这里需根据实际逻辑关联部门、角色名称，示例直接写死
                user.setDeptName("默认部门");
                user.setRoleName("默认角色");

                Result result = new Result();
                result.success = true;
                result.message = "用户信息更新成功";
                return result;
            }
        }

        Result result = new Result();
        result.success = false;
        result.message = "未找到该用户，更新失败";
        return result;
    }

    @Override
    public boolean deptInUse(int id) {
        return false;
    }

    @Override
    public boolean roleInUse(int id) {
        return false;
    }
}